/**
 * Created by zhouhua on 2016/12/28.
 */
var crypto = require('crypto');
var User = require('../models/user.js');

module.exports = {
  user: function (req, res) {
    res.render('index', {
      layout: false,
      title: '你好'
    });
  },
  post: function (req, res) {

  },
  reg: function (req, res) {
    res.render('reg', {
      layout: 'layout',
      title: '用户注册'
    });
  },
  doReg: function (req, res) {
    //检验用户两次输入的口令是否一致
    if (req.body['password-repeat'] != req.body['password']) {
      req.session.msg.error = '两次输入的口令不一致';
      return res.redirect('/reg');
    }
    //生成口令的散列值
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');
    var newUser = new User({
      name: req.body.username,
      password: password
    });
    //检查用户名是否已经存在
    User.get(newUser.name, function (err, user) {
      if (user)
        err = 'Username already exists.';
      if (err) {
        req.session.msg.error = err;
        return res.redirect('/reg');
      }
      //如果不存在则新增用户
      newUser.save(function (err) {
        if (err) {
          req.session.msg.error = err;
          return res.redirect('/reg');
        }
        req.session.user = newUser;
        req.session.msg.success = '注册成功';
        res.redirect('/');
      });
    });
  },
  login: function (req, res) {

  },
  doLogin: function (req, res) {

  },
  logout: function (req, res) {

  }
};